package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.bean.LoginBean
import com.ht.medicaltreatment.bean.RfidSample
import com.ht.medicaltreatment.bean.SampleScanBean
import com.xgst.driverlesscars.api.ApiResponse
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class SampleReturnScanViewModel : BaseViewModel() {
    //扫描功能，根据rfid编号查询样本
    fun scanOfRfid(stopCentrifugeId :String, onSuccess:(MutableList<RfidSample>?)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.scanReturnOfRfid(stopCentrifugeId)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data?.rfidSampleList)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun getUserName(name:String,psw:String?,onSuccess:(String?)->Unit){
        viewModelScope.launch {
            var bean: ApiResult<ApiResponse<LoginBean>>?
            if(psw == null){
                bean = mApi.getUserNameForUID(name)
            }else{
                bean = mApi.getUserNameForAccount(name,psw)
            }
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data?.name)
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //把样本存入冰箱
    fun returnRefrigerator(returnPerson: String,returnRemarks: String,returnRfidSampleIdList: String
                           ,returnTableName: String, onSuccess:()->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.returnRefrigerator(returnRfidSampleIdList,returnPerson,returnRemarks,returnTableName)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }
}
